package com.ruoyi.user.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.user.domain.TbUser;
import com.ruoyi.user.mapper.TbUserVerificationMapper;
import com.ruoyi.user.domain.TbUserVerification;
import com.ruoyi.user.service.ITbUserVerificationService;

/**
 * 认证审核Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-30
 */
@Service
public class TbUserVerificationServiceImpl implements ITbUserVerificationService 
{
    @Autowired
    private TbUserVerificationMapper tbUserVerificationMapper;

    /**
     * 查询认证审核
     * 
     * @param verificationId 认证审核主键
     * @return 认证审核
     */
    @Override
    public TbUserVerification selectTbUserVerificationByVerificationId(Long verificationId)
    {
        return tbUserVerificationMapper.selectTbUserVerificationByVerificationId(verificationId);
    }

    /**
     * 查询认证审核列表
     * 
     * @param tbUserVerification 认证审核
     * @return 认证审核
     */
    @Override
    public List<TbUserVerification> selectTbUserVerificationList(TbUserVerification tbUserVerification)
    {
        return tbUserVerificationMapper.selectTbUserVerificationList(tbUserVerification);
    }

    /**
     * 新增认证审核
     * 
     * @param tbUserVerification 认证审核
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbUserVerification(TbUserVerification tbUserVerification)
    {
        int rows = tbUserVerificationMapper.insertTbUserVerification(tbUserVerification);
        insertTbUser(tbUserVerification);
        return rows;
    }

    /**
     * 修改认证审核
     * 
     * @param tbUserVerification 认证审核
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbUserVerification(TbUserVerification tbUserVerification)
    {
        tbUserVerificationMapper.deleteTbUserByUserId(tbUserVerification.getVerificationId());
        insertTbUser(tbUserVerification);
        return tbUserVerificationMapper.updateTbUserVerification(tbUserVerification);
    }

    /**
     * 批量删除认证审核
     * 
     * @param verificationIds 需要删除的认证审核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbUserVerificationByVerificationIds(Long[] verificationIds)
    {
        tbUserVerificationMapper.deleteTbUserByUserIds(verificationIds);
        return tbUserVerificationMapper.deleteTbUserVerificationByVerificationIds(verificationIds);
    }

    /**
     * 删除认证审核信息
     * 
     * @param verificationId 认证审核主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbUserVerificationByVerificationId(Long verificationId)
    {
        tbUserVerificationMapper.deleteTbUserByUserId(verificationId);
        return tbUserVerificationMapper.deleteTbUserVerificationByVerificationId(verificationId);
    }

    /**
     * 新增管理用户信息
     * 
     * @param tbUserVerification 认证审核对象
     */
    public void insertTbUser(TbUserVerification tbUserVerification)
    {
        List<TbUser> tbUserList = tbUserVerification.getTbUserList();
        Long verificationId = tbUserVerification.getVerificationId();
        if (StringUtils.isNotNull(tbUserList))
        {
            List<TbUser> list = new ArrayList<TbUser>();
            for (TbUser tbUser : tbUserList)
            {
                tbUser.setUserId(verificationId);
                list.add(tbUser);
            }
            if (list.size() > 0)
            {
                tbUserVerificationMapper.batchTbUser(list);
            }
        }
    }

    /**
     * 通过认证审核
     * 
     * @param verificationId 认证审核主键
     * @return 结果
     */
    @Override
    public int approveVerification(Long verificationId)
    {
        return tbUserVerificationMapper.approveVerification(verificationId);
    }

    /**
     * 驳回认证审核
     * 
     * @param verificationId 认证审核主键
     * @param rejectReason 驳回原因
     * @return 结果
     */
    @Override
    public int rejectVerification(Long verificationId, String rejectReason) {
        Map<String, Object> params = new HashMap<>();
        params.put("verificationId", verificationId);
        params.put("rejectReason", rejectReason);
        return tbUserVerificationMapper.rejectVerification(params);
    }
}